import { Button, Label, Node } from 'cc';
import { EventName } from '../consts/events';
import { AudioPath } from '../consts/resources';
import Utils from '../utils';
import { UiBasic } from './basic';

// 游戏界面
export class UiPlayGame extends UiBasic {
    private isPlay: boolean = false;
    private btnOn: Node;
    private btnOff: Node;
    private lbScore: Label;
    private lbHP: Label;
    protected onLoad(): void {
        this.btnOn = this.node.getChildByPath('switch/on');
        this.btnOff = this.node.getChildByPath('switch/off');
        this.btnOn.on(Button.EventType.CLICK, this.onSwitchClick, this);
        this.btnOff.on(Button.EventType.CLICK, this.onSwitchClick, this);
        Utils.events.on(EventName.ScoreUpdate, this.onScoreUpdate, this);
        Utils.events.on(EventName.HpUpdate, this.onHpUpdate, this);
        this.lbScore = this.node.getChildByPath('score').getComponent(Label);
        this.lbHP = this.node.getChildByPath('hp').getComponent(Label);
        this.node.setPosition(0, 0);
        this.hide();
    }
    protected onEnable(): void {
        Utils.audio.play(AudioPath.Bg, true);
    }
    protected onDisable(): void {
        Utils.audio.stop();
    }

    private onScoreUpdate(score: number) {
        this.lbScore.string = score.toString();
    }
    private onHpUpdate(hp: number) {
        this.lbHP.string = `X ${hp}`;
    }

    private onSwitchClick() {
        if (this.isPlay) {
            Utils.audio.pause();
            Utils.events.emit(EventName.PauseGame);
        } else {
            Utils.audio.resume();
            Utils.events.emit(EventName.ResumeGame);
        }
    }
    public setPlay(play: boolean) {
        this.isPlay = play;
        this.btnOn.active = this.isPlay;
        this.btnOff.active = !this.isPlay;
    }
}
